from bs4 import BeautifulSoup

html = '''
<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title" name="dromouse"><b>The Dormouse's story</b></p>
<p class="story">Once upon a time there were three little sisters;and their names were
<a href="http://example.com/elsie" class="sister" id="link1"><!--Elsie--></a>,
<a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and
<a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;
    and they lived at the bottom of a well.</p>>
<p class="stroy">...</p>
'''
soup = BeautifulSoup(html,'lxml')
print(soup.prettify())
#输出 title节点的文本内容
print(soup.title.string)
#获取节点的名称
print(soup.title.name)
#获取节点的属性
print(soup.p.attrs)
#获取属性名那么name的p节点
print(soup.p.attrs['name'])
print(soup.p['name'])
print(soup.p.string)
print(soup.p)
#嵌套选择
print(soup.head.title.string)
#关联选择
print(soup.p.contents)
print(soup.p.children)
print(soup.p.parent)
#下一个兄弟元素
print(soup.p.next_slibings)
#上一个兄弟元素
print(soup.p.previous_slibings)